package search.lucene;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.TopFieldDocs;
import org.apache.lucene.util.Version;

/**
 *
 * @author cosmin
 */
public class Filter extends Abstract {

    /**
     * Static instance used for singleton
     */
    protected static Filter _instance = null;

    /**
     * Singleton method
     *
     * @return One single instance
     */
    public static Filter getInstance() {
        if (null == Filter._instance) {
            Filter._instance = new Filter();
        }
        return Filter._instance;
    }

    public Filter() {
        super();
    }

    public boolean doSearch(String brand, String county, String tip, String an, String stare, String culoare, String usi, String carburant, String caroserie, String cutie) {
        boolean hasResult = true;
        List<String> paramsList = new ArrayList<>();
        String queryText = "";
        if (null != brand && !brand.equalsIgnoreCase("")) {
            paramsList.add("title");
            if (!queryText.equalsIgnoreCase("")) {
                queryText += " AND ";
            }
            queryText += "title: \"" + brand + "\"";
        }
        if (null != county && !county.equalsIgnoreCase("")) {
            paramsList.add("address");
            if (!queryText.equalsIgnoreCase("")) {
                queryText += " AND ";
            }
            queryText += "address: \"" + county + "\"";
        }
        if (null != tip && !tip.equalsIgnoreCase("")) {
            paramsList.add("Tip");
            if (!queryText.equalsIgnoreCase("")) {
                queryText += " AND ";
            }
            queryText += "Tip: \"" + tip + "\"";
        }
        if (null != an && !an.equalsIgnoreCase("")) {
            paramsList.add("An");
            if (!queryText.equalsIgnoreCase("")) {
                queryText += " AND ";
            }
            queryText += "An: \"" + an + "\"";
        }
        if (null != stare && !stare.equalsIgnoreCase("")) {
            paramsList.add("Stare_tehnica");
            if (!queryText.equalsIgnoreCase("")) {
                queryText += " AND ";
            }
            queryText += "Stare_tehnica: \"" + stare + "\"";
        }
        if (null != culoare && !culoare.equalsIgnoreCase("")) {
            paramsList.add("Culoare");
            if (!queryText.equalsIgnoreCase("")) {
                queryText += " AND ";
            }
            queryText += "Culoare: \"" + culoare + "\"";
        }
        if (null != usi && !usi.equalsIgnoreCase("")) {
            paramsList.add("Numar_de_usi");
            if (!queryText.equalsIgnoreCase("")) {
                queryText += " AND ";
            }
            queryText += "Numar_de_usi: \"" + usi + "\"";
        }
        if (null != carburant && !carburant.equalsIgnoreCase("")) {
            paramsList.add("Carburant");
            if (!queryText.equalsIgnoreCase("")) {
                queryText += " AND ";
            }
            queryText += "Carburant: \"" + carburant + "\"";
        }
        if (null != caroserie && !caroserie.equalsIgnoreCase("")) {
            paramsList.add("Caroserie");
            if (!queryText.equalsIgnoreCase("")) {
                queryText += " AND ";
            }
            queryText += "Caroserie: \"" + caroserie + "\"";
        }
        if (null != cutie && !cutie.equalsIgnoreCase("")) {
            paramsList.add("Cutie_de_viteze");
            if (!queryText.equalsIgnoreCase("")) {
                queryText += " AND ";
            }
            queryText += "Cutie_de_viteze: \"" + cutie + "\"";
        }
        String[] params = new String[paramsList.size()];
        for (int i = 0; i < paramsList.size(); i++) {
            params[i] = paramsList.get(i);
        }
        paramsList = null;
        try {
            QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_40, params, this._analyzer);
            org.apache.lucene.search.Query query = queryParser.parse(queryText);
            SortField sortField = new SortField("dateReal", SortField.Type.LONG, true);
            Sort sortFiltre = new Sort(sortField);
            TopFieldDocs topFieldDocs = this._indexSearcher.search(query, null, this.hitsPerPage, sortFiltre);
            this.hits = topFieldDocs.scoreDocs;
            this.totalHits = topFieldDocs.totalHits;
            this.hasResults = true;
        } catch (IOException | ParseException ex) {
            hasResult = false;
            Logger.getLogger(Query.class.getName()).log(Level.SEVERE, null, ex);
        }
        return hasResult;
    }
}
